class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
class Solution(object):
    def verticalTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        st = []
        def dfs(node, i, j):
            st.append([j, i, node.val])
            if node.left:
                dfs(node.left, i + 1, j - 1)
            if node.right:
                dfs(node.right, i + 1, j + 1)

        dfs(root, 0, 0)
        st.sort()
        last_j = -float('inf')
        ans = []
        for j, _, val in st:
            if j > last_j:
                last_j = j
                ans.append([val])
            else:
                ans[-1].append(val)
        return ans


